<template>
  <div class="import-table-container">
    <el-form :inline="true" :model="searchform" label-width="80px">
      <el-form-item>
        <el-input placeholder="请输入数据库" clearable v-model="searchform.db" size="mini"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="handleQuery">查询</el-button>
        <el-button type="primary" size="mini" @click="handleReturn">返回</el-button>
      </el-form-item>
    </el-form>
    <div class="table">
      <el-table height="100%" :data="tableData" border>
        <el-table-column prop="tableName" label="数据表" align="center" width="200"></el-table-column>
        <el-table-column prop="tableComment" label="注释" align="center"></el-table-column>
        <el-table-column prop="tableEngine" label="引擎" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="handleImport(scope.row)">导入</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchform.pageNum"
      :page-sizes="[10, 15, 20]"
      :page-size="searchform.pageSize"
      layout="total, sizes, prev, pager, next"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
import { selectTableFromDb, importTableToSystem } from "@/api/gen";
export default {
  name: "importAndSave",
  components: {},
  props: {
    value: Boolean
  },
  data() {
    return {
      tableData: [],
      searchform: {
        pageNum: 1,
        pageSize: 10,
        db: ""
      },
      total: 0
    };
  },
  methods: {
    handleQuery() {
      this.selectTableFromDb();
    },
    async selectTableFromDb() {
      const { data } = await selectTableFromDb(this.searchform);
      this.tableData = data.data;
      this.searchform.pageNum = data.pageNum;
      this.searchform.pageSize = data.pageSize;
      this.total = data.total;
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.searchform.pageNum = val;
      this.selectTableFromDb(this.searchform);
    },
    async handleImport(row) {
      await importTableToSystem({ ...row, db: this.searchform.db });
    },
    handleReturn() {
      this.$router.back();
    },
    cancel() {
      this.$emit("input", false);
    }
  }
};
</script>

<style lang="scss" scoped>
.import-table-container {
  background-color: #fff;
  height: 100%;
  margin: 30px;
  padding: 10px;
}
.table {
  height: 400px;
}
.el-pagination {
  margin-top: 40px;
  text-align: center;
}
</style>